## Title: Replication file for the appendix of Somer-Topcu and Weitzel 
## Date: 2021-11-23
## Purpose: File replicates Table A2-1 through Table A8-1 from the appendix 
## Data used: df_cps_monadic.csv and df_cps_dyadic.csv 
## Note: In order to facilitate replication with other software the glmer models from the lme4 package follow 
##       the default specifications in Stata. We set nAGQ = 7.

## Load Libraries
library("rio")           ## data import
library("tidyverse")     ## data wrangling
library("lme4")          ## model estimation
library("texreg")        ## table export
library("sjPlot")        ## model visualization
library("ggpubr")        ## combining the plots


## Layout defaults
theme_update(text = element_text(size=20))

## Load the data 
## two data frames are loaded. One for the monadic models and one for the dyadic ones.
df_monadic <- read_csv("df_cps_monadic.csv") 
df_dyadic  <- read_csv("df_cps_dyadic.csv")


################################### 
## Table A2-1: Parties with less than five valence statements 
taba21 <- read_csv("df_taba21.csv")
taba21

################################### 
## Table A2-2: Replication of Table 3 With at Least 10 Statements  
## Model specification
model_a22a_monadic <- as.formula(votedummy ~ share_neg_disc_by_others_all + votet1_pollt1_diff + poll_campaign_start + pid_dummy_no + last_govt + polkno + age + gender + as.factor(ccode) + (1 | party_election))
model_a22b_monadic <- as.formula(votedummy ~ share_neg_disc_by_others_all + votet1_pollt1_diff + poll_campaign_start + last_govt + polkno + age + gender + as.factor(ccode) + (1 | party_election))
model_a22c_dyadic  <- as.formula(votedummy_other ~  share_nov + votet1_pollt1_diff_other + other_poll_campaign_start + polkno + age + gender + other_last_govt + as.factor(ccode) + (1 | party_election))
model_a22c_dyadic  <- as.formula(votedummy_other ~ share_nov_others + share_nov + votet1_pollt1_diff_other + other_poll_campaign_start + polkno + age + gender + other_last_govt + as.factor(ccode) + (1 | party_election))

## Model estimation
a22_c1 <- glmer(model_a22a_monadic, data = subset(df_monadic, allstatements_all > 9), nAGQ = 7, family = binomial(link = "logit"))
a22_c2 <- glmer(model_a22b_monadic, data = subset(df_monadic, allstatements_all > 9 & pid_dummy_no == 1), nAGQ = 7, family = binomial(link = "logit"))
a22_c3 <- glmer(model_a22c_dyadic,  data = subset(df_dyadic,  allstatements_all > 9 & copartisan_subject == 1 & party_election != "82602015_51951"), nAGQ = 7, family = binomial(link = "logit"))
a22_c4 <- glmer(model_a22b_monadic, data = subset(df_monadic, allstatements_all > 9 & nonpartisan == 1), nAGQ = 7, family = binomial(link = "logit"))

## Results
screenreg(list(a22_c1, a22_c2, a22_c3, a22_c4), 
       omit.coef = "ccode",
       stars = c(0.05),
       custom.note = "Note: Dependent variable is vote choice. Standard errors in parentheses. The models \n include party-election random intercepts and country fixed effects. Reported are the standard log odds coefficients. \n %stars.",
       custom.coef.names = c("Intercept", "Received Attacks", "Delta Party Performance", "Party Performance",
                             "Copartisan", "Government t-1", "Political Knowledge", "Age", "Male", "Others’ attacks",
                             "Party’s Attacks",  "Delta Party Performance", "Party Performance","Government t-1"),
       reorder.coef = c(2, 11, 10,3,4,5,6,7,8,9,1),
       custom.gof.names = c(NA, NA, NA, "Observations", "Groups", "Var: Groups (Intercept)"),
       custom.header = list("All" = 1, "Copartisans, target" = 2, "Copartisans, attacker" = 3, "Nonpartisans" = 4))

rm(model_a22a_monadic, model_a22b_monadic, model_a22c_dyadic)
rm(a22_c1, a22_c2, a22_c3, a22_c4)

################################### 
## Table A3-1: Replication of Table 3 With Seat Shares
## Model specification
model_a31a_monadic <- as.formula(votedummy ~ share_neg_disc_by_others_all + seat_share_lag + pid_dummy_no + last_govt + polkno + age + gender + as.factor(ccode) + (1 | party_election))
model_a31b_monadic <- as.formula(votedummy ~ share_neg_disc_by_others_all + seat_share_lag + last_govt + polkno + age + gender + as.factor(ccode) + (1 | party_election))
model_a31c_dyadic  <- as.formula(votedummy_other ~ share_nov_others + share_nov + seat_share_lag_other + polkno + age + gender + other_last_govt + as.factor(ccode) + (1 | party_election))

## Model estimation
a31_c1 <- glmer(model_a31a_monadic, data = subset(df_monadic), nAGQ = 7, family = binomial(link = "logit"))
a31_c2 <- glmer(model_a31b_monadic, data = subset(df_monadic, pid_dummy_no == 1), nAGQ = 7, family = binomial(link = "logit"))
a31_c3 <- glmer(model_a31c_dyadic,  data = subset(df_dyadic, copartisan_subject == 1), nAGQ = 7, family = binomial(link = "logit"))
a31_c4 <- glmer(model_a31b_monadic, data = subset(df_monadic, nonpartisan == 1), nAGQ = 7, family = binomial(link = "logit"))

## Results
screenreg(list(a31_c1, a31_c2, a31_c3, a31_c4), 
       omit.coef = "ccode",
       stars = c(0.05),
       custom.note = "Note: Dependent variable is vote choice. Standard errors in parentheses. The models \n include party-election random intercepts and country fixed effects. Reported are the standard log odds coefficients. \n %stars.",
       custom.coef.names = c("Intercept", "Received Attacks", "Seat share, lag",
                             "Copartisan", "Government t-1", "Political Knowledge", "Age", "Male", "Others’ attacks",
                             "Party’s Attacks",  "Seat share, lag","Government t-1"),
       reorder.coef = c(2,10,9,3,4,5,6,7,8,1),
       custom.gof.names = c(NA, NA, NA, "Observations", "Groups", "Var: Groups (Intercept)"),
       custom.header = list("All" = 1, "Copartisans, target" = 2, "Copartisans, attacker" = 3, "Nonpartisans" = 4))

rm(model_a31a_monadic, model_a31b_monadic, model_a31c_dyadic)
rm(a31_c1,a31_c2, a31_c3, a31_c4)

################################### 
## Table A4-1: Robustness of Table 3 Without Individual-Level Controls
## Model specification
model_a41a_monadic <- as.formula(votedummy ~ share_neg_disc_by_others_all + votet1_pollt1_diff + poll_campaign_start + as.factor(pid_dummy_no) + as.factor(last_govt) + as.factor(ccode) + (1 | party_election))
model_a41b_monadic <- as.formula(votedummy ~ share_neg_disc_by_others_all + votet1_pollt1_diff + poll_campaign_start + as.factor(last_govt) + as.factor(ccode) + (1 | party_election))
model_a41c_dyadic  <- as.formula(votedummy_other ~ share_nov_others + share_nov + votet1_pollt1_diff_other + other_poll_campaign_start + as.factor(other_last_govt) + as.factor(ccode) + (1 | party_election))

## Model estimation
a41_c1 <- glmer(model_a41a_monadic, data = subset(df_monadic), nAGQ = 7, family = binomial(link = "logit"))
a41_c2 <- glmer(model_a41b_monadic, data = subset(df_monadic, pid_dummy_no == 1), nAGQ = 7, family = binomial(link = "logit"))
a41_c3 <- glmer(model_a41c_dyadic,  data = subset(df_dyadic, copartisan_subject == 1), nAGQ = 7, family = binomial(link = "logit"))
a41_c4 <- glmer(model_a41b_monadic, data = subset(df_monadic, nonpartisan == 1), nAGQ = 7, family = binomial(link = "logit"))

## Model reporting 
screenreg(list(a41_c1, a41_c2, a41_c3, a41_c4), 
          omit.coef = "ccode",
          stars = c(0.05),
          custom.note = "Note: Dependent variable is vote choice. Standard errors in parentheses. The models \n include party-election random intercepts and country fixed effects. Reported are the standard log odds coefficients. \n %stars.",
          custom.coef.names = c("Intercept", "Received Attacks", "Delta Party Performance", "Party Performance",
                                "Copartisan", "Government t-1", "Others' attacks",
                                "Party's Attacks",  "Delta Party Performance", "Party Performance","Government t-1"),
          reorder.coef = c(2, 8, 7, 3, 4, 5, 6, 1),
          custom.gof.names = c(NA, NA, NA, "Observations", "Groups", "Var: Groups (Intercept)"),
          custom.header = list("All" = 1, "Copartisans, target" = 2, "Copartisans, attacker" = 3, "Nonpartisans" = 4))

rm(model_a41a_monadic, model_a41b_monadic, model_a41c_dyadic)
rm(a41_c1, a41_c2, a41_c3, a41_c4)

################################### 
## Table A4-2: Replication of Table 3 With Political Knowledge Interaction
## Model specification
model_a42a_monadic <- as.formula(votedummy ~ share_neg_disc_by_others_all*polkno + votet1_pollt1_diff + poll_campaign_start + pid_dummy_no + last_govt + age + gender + as.factor(ccode) + (1 | party_election))
model_a42b_monadic <- as.formula(votedummy ~ share_neg_disc_by_others_all*polkno + votet1_pollt1_diff + poll_campaign_start + last_govt + age + gender + as.factor(ccode) + (1 | party_election))
model_a42c_dyadic  <- as.formula(votedummy_other ~ share_nov_others*polkno + share_nov*polkno + votet1_pollt1_diff_other + other_poll_campaign_start + age + gender + other_last_govt + as.factor(ccode) + (1 | party_election))

## Model estimation
a42_c1 <- glmer(model_a42a_monadic, data = subset(df_monadic), nAGQ = 7, family = binomial(link = "logit"))
a42_c2 <- glmer(model_a42b_monadic, data = subset(df_monadic, pid_dummy_no == 1), nAGQ = 7, family = binomial(link = "logit"))
a42_c3 <- glmer(model_a42c_dyadic,  data = subset(df_dyadic, copartisan_subject == 1), nAGQ = 7, family = binomial(link = "logit"))
a42_c4 <- glmer(model_a42b_monadic, data = subset(df_monadic, nonpartisan == 1), nAGQ = 7, family = binomial(link = "logit"))

## Results
screenreg(list(a42_c1, a42_c2, a42_c3, a42_c4), 
       omit.coef = "ccode",
       stars = c(0.05),
       custom.coef.names = c("Intercept", "Received Attacks", "Political Knowledge", "Delta Party Performance", "Party Performance",
                             "Copartisan", "Government t-1", "Age", "Male", "Received Attacks*Political Knowledge",
                             "Others' Attacks", "Party’s Attacks", "Delta Party Performance", "Party Performance","Government t-1",
                             "Others' Attacks*Political Knowledge","Party’s Attacks*Political Knowledge"),
       reorder.coef = c(2, 12, 3, 10, 14, 11, 13, 4, 5, 6, 7, 8, 9, 1),
       custom.gof.names = c(NA, NA, NA, "Observations", "Groups", "Var: Groups (Intercept)"),
       custom.header = list("All" = 1, "Copartisans, target" = 2, "Copartisans, attacker" = 3, "Nonpartisans" = 4))

rm(model_a42a_monadic, model_a42b_monadic, model_a42c_dyadic)
rm(a42_c1, a42_c2, a42_c3, a42_c4)

################################### 
## Table A4-3: Replication of Table 3 With Gender Interaction
## Model specification
model_a43a_monadic <- as.formula(votedummy ~ share_neg_disc_by_others_all*gender + votet1_pollt1_diff + poll_campaign_start + pid_dummy_no + last_govt + polkno + age  + as.factor(ccode) + (1 | party_election))
model_a43b_monadic <- as.formula(votedummy ~ share_neg_disc_by_others_all*gender + votet1_pollt1_diff + poll_campaign_start + last_govt + polkno + age + as.factor(ccode) + (1 | party_election))
model_a43c_dyadic  <- as.formula(votedummy_other ~ share_nov_others*gender + share_nov*gender + votet1_pollt1_diff_other + other_poll_campaign_start + polkno + age + other_last_govt + as.factor(ccode) + (1 | party_election))

## Model estimation
a43_c1 <- glmer(model_a43a_monadic, data = subset(df_monadic), nAGQ = 7, family = binomial(link = "logit"))
a43_c2 <- glmer(model_a43b_monadic, data = subset(df_monadic, pid_dummy_no == 1), nAGQ = 7, family = binomial(link = "logit"))
a43_c3 <- glmer(model_a43c_dyadic,  data = subset(df_dyadic, copartisan_subject == 1), nAGQ = 7, family = binomial(link = "logit"))
a43_c4 <- glmer(model_a43b_monadic, data = subset(df_monadic, nonpartisan == 1), nAGQ = 7, family = binomial(link = "logit"))

## Results
screenreg(list(a43_c1, a43_c2, a43_c3, a43_c4), 
       omit.coef = "ccode",
       stars = c(0.05),
       custom.coef.names = c("Intercept", "Received Attacks", "Male", "Delta Party Performance", "Party Performance",
                             "Copartisan", "Government t-1", "Political Knowledge", "Age", "Received Attacks*Male",
                             "Others' Attacks", "Party’s Attacks", "Delta Party Performance", "Party Performance","Government t-1",
                             "Others' Attacks*Male","Party’s Attacks*Male"),
       reorder.coef = c(2,12,3,10,14,11,13,4,5,6,7,8,9,1),
       custom.gof.names = c(NA, NA, NA, "Observations", "Groups", "Var: Groups (Intercept)"),
       custom.header = list("All" = 1, "Copartisans, target" = 2, "Copartisans, attacker" = 3, "Nonpartisans" = 4))

rm(model_a43a_monadic, model_a43b_monadic, model_a43c_dyadic)
rm(a43_c1, a43_c2, a43_c3, a43_c4)

################################### 
## Table A4-4: Replication of Table 3 With Age Interaction
## Model specification
model_a44a_monadic <- as.formula(votedummy ~ share_neg_disc_by_others_all*age + votet1_pollt1_diff + poll_campaign_start + pid_dummy_no + last_govt + polkno + gender  + as.factor(ccode) + (1 | party_election))
model_a44b_monadic <- as.formula(votedummy ~ share_neg_disc_by_others_all*age + votet1_pollt1_diff + poll_campaign_start + last_govt + polkno + gender + as.factor(ccode) + (1 | party_election))
model_a44c_dyadic  <- as.formula(votedummy_other ~ share_nov_others*age + share_nov*age + votet1_pollt1_diff_other + other_poll_campaign_start + polkno + gender + other_last_govt + as.factor(ccode) + (1 | party_election))

## Model estimation
a44_c1 <- glmer(model_a44a_monadic, data = subset(df_monadic), nAGQ = 7, family = binomial(link = "logit"))
a44_c2 <- glmer(model_a44b_monadic, data = subset(df_monadic, pid_dummy_no == 1), nAGQ = 7, family = binomial(link = "logit"))
a44_c3 <- glmer(model_a44c_dyadic,  data = subset(df_dyadic, copartisan_subject == 1), nAGQ = 7, family = binomial(link = "logit"))
a44_c4 <- glmer(model_a44b_monadic, data = subset(df_monadic, nonpartisan == 1), nAGQ = 7, family = binomial(link = "logit"))

## Results
screenreg(list(a44_c1, a44_c2, a44_c3, a44_c4), 
       omit.coef = "ccode",
       stars = c(0.05),
       custom.coef.names = c("Intercept", "Received Attacks", "Age", "Delta Party Performance", "Party Performance",
                             "Copartisan", "Government t-1", "Political Knowledge", "Male", "Received Attacks*Age",
                             "Others' Attacks", "Party’s Attacks", "Delta Party Performance", "Party Performance","Government t-1",
                             "Others' Attacks*Age","Party’s Attacks*Age"),
       reorder.coef = c(2,12,3,10,14,11,13,4,5,6,7,8,9,1),
       custom.gof.names = c(NA, NA, NA, "Observations", "Groups", "Var: Groups (Intercept)"),
       custom.header = list("All" = 1, "Copartisans, target" = 2, "Copartisans, attacker" = 3, "Nonpartisans" = 4))

rm(model_a44a_monadic, model_a44b_monadic, model_a44c_dyadic)
rm(a44_c1, a44_c2, a44_c3, a44_c4)

################################### 
## Table A4-5: Replication of Table 3 With Incumbency Interaction
## Model specification
model_a45a_monadic <- as.formula(votedummy ~ share_neg_disc_by_others_all*last_govt + votet1_pollt1_diff + poll_campaign_start + pid_dummy_no + polkno + age + gender  + as.factor(ccode) + (1 | party_election))
model_a45b_monadic <- as.formula(votedummy ~ share_neg_disc_by_others_all*last_govt + votet1_pollt1_diff + poll_campaign_start + polkno + age + gender + as.factor(ccode) + (1 | party_election))
model_a45c_dyadic  <- as.formula(votedummy_other ~ share_nov_others*other_last_govt + share_nov*other_last_govt + votet1_pollt1_diff_other + other_poll_campaign_start + polkno + age + gender + as.factor(ccode) + (1 | party_election))
model_a45d_dyadic  <- as.formula(votedummy_other ~ share_nov_others + share_nov*subject_last_govt + votet1_pollt1_diff_other + other_poll_campaign_start + polkno + age + gender + as.factor(ccode) + (1 | party_election))

## Model estimation
a45_c1 <- glmer(model_a45a_monadic, data = subset(df_monadic), nAGQ = 7, family = binomial(link = "logit"))
a45_c2 <- glmer(model_a45b_monadic, data = subset(df_monadic, pid_dummy_no == 1), nAGQ = 7, family = binomial(link = "logit"))
a45_c3 <- glmer(model_a45c_dyadic,  data = subset(df_dyadic, copartisan_subject == 1), nAGQ = 7, family = binomial(link = "logit"))
a45_c4 <- glmer(model_a45b_monadic, data = subset(df_monadic, nonpartisan == 1), nAGQ = 7, family = binomial(link = "logit"))

## Results
screenreg(list(a45_c1, a45_c2, a45_c3, a45_c4), 
       omit.coef = "ccode",
       stars = c(0.05),
       custom.coef.names = c("Intercept", "Received Attacks", "Government t-1", "Delta Party Performance", "Party Performance",
                             "Copartisan", "Political Knowledge", "Age", "Male", "Received Attacks*Government t-1",
                             "Others' Attacks",  "Government t-1", "Party’s Attacks", "Delta Party Performance", "Party Performance",
                             "Others' Attacks*Government t-1","Party’sAttacks*Government t-1"),
       reorder.coef = c(2,12,3,10,14,11,13,4,5,6,7,8,9,1),
       custom.gof.names = c(NA, NA, NA, "Observations", "Groups", "Var: Groups (Intercept)"),
       custom.header = list("All" = 1, "Copartisans, target" = 2, "Copartisans, attacker" = 3, "Nonpartisans" = 4))

rm(model_a45a_monadic, model_a45b_monadic, model_a45c_dyadic)
rm(a45_c1, a45_c2, a45_c3, a45_c4)

################################### 
## Table A5-1: Robustness of Table 3 With Attack Volume Control
## Model specification
model_a51a_monadic <- as.formula(votedummy ~ share_neg_disc_by_others_all + sum_nov_received + votet1_pollt1_diff + poll_campaign_start + as.factor(pid_dummy_no) + as.factor(last_govt) + polkno + age + as.factor(gender) + as.factor(ccode) + (1 | party_election))
model_a51b_monadic <- as.formula(votedummy ~ share_neg_disc_by_others_all + sum_nov_received  + votet1_pollt1_diff + poll_campaign_start + as.factor(last_govt) + polkno + age + as.factor(gender) + as.factor(ccode) + (1 | party_election))
model_a51c_dyadic  <- as.formula(votedummy_other ~ share_nov_others + share_nov + sum_nov_total + sum_nov_total_others + votet1_pollt1_diff_other + other_poll_campaign_start + polkno + age + as.factor(gender) + as.factor(other_last_govt) + as.factor(ccode) + (1 | party_election))

## Model estimation
a51_c1 <- glmer(model_a51a_monadic, data = subset(df_monadic), nAGQ = 7, family = binomial(link = "logit"))
a51_c2 <- glmer(model_a51b_monadic, data = subset(df_monadic, pid_dummy_no == 1), nAGQ = 7, family = binomial(link = "logit"))
a51_c3 <- glmer(model_a51c_dyadic,  data = subset(df_dyadic, copartisan_subject == 1), nAGQ = 7, family = binomial(link = "logit"))
a51_c4 <- glmer(model_a51b_monadic, data = subset(df_monadic, nonpartisan == 1), nAGQ = 7, family = binomial(link = "logit"))

## Results
screenreg(list(a51_c1, a51_c2, a51_c3, a51_c4), 
       omit.coef = "ccode",
       stars = c(0.05),
       custom.coef.names = c("Intercept", "Received Attacks (Share)", "Received Attacks (Volume)", "Delta Party Performance", "Party Performance",
                           "Copartisan", "Government t-1", "Political Knowledge", "Age", "Male", "Others' Attacks (Share)",
                             "Party’s  Attacks (Share)", "Party’s  Attacks (Volume)",
                             "Others' Attacks (Volume)",  "Delta Party Performance", "Party Performance","Government t-1"),
      reorder.coef = c(2,3, 12,13,11,14,4,5,6,7,8,9,10,1),
       custom.gof.names = c(NA, NA, NA, "Observations", "Groups", "Var: Groups (Intercept)"),
       custom.header = list("All" = 1, "Copartisans, target" = 2, "Copartisans, attacker" = 3, "Nonpartisans" = 4))

rm(model_a51a_monadic, model_a51b_monadic, model_a51c_dyadic)
rm(a51_c1, a51_c2, a51_c3, a51_c4)

################################### 
## Table A6-1: Interaction of Attacks with Absolute Ideological Distance
## Model specification
model_a61a1_dyadic <- as.formula(votedummy_other ~ share_nov*dist_ideol_abs +  votet1_pollt1_diff_other + other_poll_campaign_start + polkno + age + as.factor(gender) + as.factor(other_last_govt) + as.factor(ccode) + (1 | party_election))
model_a61a_dyadic  <- as.formula(votedummy_other ~ share_nov*dist_ideol_abs + share_nov_others*dist_ideol_abs+ votet1_pollt1_diff_other + other_poll_campaign_start + polkno + age + as.factor(gender) + as.factor(other_last_govt) + as.factor(ccode) + (1 | party_election))
model_a61b_dyadic  <- as.formula(votedummy_other ~ share_nov*dist_ideol_abs + votet1_pollt1_diff_other + other_poll_campaign_start + polkno + age + as.factor(gender) + as.factor(other_last_govt) + as.factor(ccode) + (1 | party_election))

## Model estimation
a61_c1 <- glmer(model_a61a1_dyadic, data = subset(df_dyadic), nAGQ = 7, family = binomial(link = "logit"))
a61_c2 <- glmer(model_a61b_dyadic, data = subset(df_dyadic, copartisan_other == 1), nAGQ = 7, family = binomial(link = "logit"))
a61_c3 <- glmer(model_a61a1_dyadic, data = subset(df_dyadic, copartisan_subject == 1), nAGQ = 7, family = binomial(link = "logit"))
a61_c4 <- glmer(model_a61b_dyadic, data = subset(df_dyadic, nonpartisan_all == 1), nAGQ = 7, family = binomial(link = "logit"))

## Columns 1, 2, 4 
screenreg(list(a61_c1, a61_c2, a61_c4), 
       omit.coef = "ccode",
       stars = c(0.05),
       custom.coef.names = c("Intercept", "Received Attacks", "Abs. Ideolog. Dist.", "Delta Party Performance", "Party Performance",
                             "Political Knowledge", "Age", "Male", "Government t-1", "Received Attacks*Abs. Ideolog. Dist."),
       reorder.coef = c(2,3,10,4,5,6,7,8,9,1),
       custom.gof.names = c(NA, NA, NA, "Observations", "Groups", "Var: Groups (Intercept)"),
       custom.header = list("All" = 1, "Copartisans, target" = 2, "Nonpartisans" = 3))

## Column 3
screenreg(list(a61_c3), 
          omit.coef = "ccode",
          stars = c(0.05),
          custom.coef.names = c("Intercept", "Party’s Attacks", "Abs. Ideolog. Dist.", "Delta Party Performance", "Party Performance",
                                "Political Knowledge", "Age", "Male", "Government t-1", "Party’s Attacks*Abs. Ideolog. Dist."),
          reorder.coef = c(2,3,10,4,5,6,7,8,9,1),
          custom.gof.names = c(NA, NA, NA, "Observations", "Groups", "Var: Groups (Intercept)"),
          custom.header = list("Copartisans, attacker" = 1))

rm(model_a61a_monadic, model_a61b_monadic, model_a61a1_dyadic)
rm(a61_c1, a61_c2, a61_c3, a61_c4)

################################### 
## Table A6-2: Interaction of Attacks with Vote Share of Attacker
## Model specification
model_a62a1_dyadic <- as.formula(votedummy_other ~ share_nov*subject_pervote_lag + votet1_pollt1_diff_other + other_poll_campaign_start + polkno + age + as.factor(gender) + as.factor(other_last_govt) + as.factor(ccode) + (1 | party_election))
model_a62a_dyadic  <- as.formula(votedummy_other ~ share_nov*subject_pervote_lag + votet1_pollt1_diff_other + other_poll_campaign_start + polkno + age + as.factor(gender) + as.factor(other_last_govt) + as.factor(ccode) + (1 | party_election))
model_a62b_dyadic  <- as.formula(votedummy_other ~ share_nov*subject_pervote_lag + votet1_pollt1_diff_other + other_poll_campaign_start + polkno + age + as.factor(gender) + as.factor(other_last_govt) + as.factor(ccode) + (1 | party_election))

## Model estimation
a62_c1 <- glmer(model_a62a1_dyadic, data = subset(df_dyadic), nAGQ = 7, family = binomial(link = "logit"))
a62_c2 <- glmer(model_a62a_dyadic, data = subset(df_dyadic, copartisan_other == 1), nAGQ = 7, family = binomial(link = "logit"))
a62_c3 <- glmer(model_a62a1_dyadic, data = subset(df_dyadic, copartisan_subject == 1), nAGQ = 7, family = binomial(link = "logit"))
a62_c4 <- glmer(model_a62b_dyadic, data = subset(df_dyadic, nonpartisan_all == 1), nAGQ = 7, family = binomial(link = "logit"))

## Columns 1, 2, 4 
screenreg(list(a62_c1,a62_c2,a62_c4), 
       omit.coef = "ccode",
       stars = c(0.05),
       custom.coef.names = c("Intercept", "Received Attacks", "Vote Share-Attacker", "Delta Party Performance", "Party Performance",
                             "Political Knowledge", "Age", "Male", "Government t-1", "Received Attacks*Vote Share-Attacker"),
       reorder.coef = c(2,3,10,4,5,6,7,8,9,1),
       custom.gof.names = c(NA, NA, NA, "Observations", "Groups", "Var: Groups (Intercept)"),
       custom.header = list("All" = 1, "Copartisans, target" = 2, "Nonpartisans" = 3))

## Column 3
screenreg(list(a62_c3), 
          omit.coef = "ccode",
          stars = c(0.05),
          custom.coef.names = c("Intercept", "Party’s Attacks", "Vote Share-Attacker", "Delta Party Performance", "Party Performance",
                                "Political Knowledge", "Age", "Male", "Government t-1", "Party’s Attacks*Vote Share-Attacker"),
          reorder.coef = c(2,3,10,4,5,6,7,8,9,1),
          custom.gof.names = c(NA, NA, NA, "Observations", "Groups", "Var: Groups (Intercept)"),
          custom.header = list("Copartisans, attacker" = 1))

rm(model_a62a_monadic, model_a62b_monadic, model_a62c_dyadic)
rm(a62_c1, a62_c2, a62_c3, a62_c4)

################################### 
## Table A6-3: Testing The Effect of Governing Parties’ Attacks
## Model specification
model_a63a1_dyadic  <- as.formula(votedummy_other ~ share_nov*subject_last_govt  + votet1_pollt1_diff_other + other_poll_campaign_start + polkno + age + as.factor(gender) + as.factor(other_last_govt) + as.factor(ccode) + (1 | party_election))
model_a63a_dyadic  <- as.formula(votedummy_other ~ share_nov*subject_last_govt  + share_nov_others*subject_last_govt  + votet1_pollt1_diff_other + other_poll_campaign_start + polkno + age + as.factor(gender) + as.factor(other_last_govt) + as.factor(ccode) + (1 | party_election))
model_a63b_dyadic  <- as.formula(votedummy_other ~ share_nov*subject_last_govt  + votet1_pollt1_diff_other + other_poll_campaign_start + polkno + age + as.factor(gender) + as.factor(other_last_govt) + as.factor(ccode) + (1 | party_election))

## Model estimation
a63_c1 <- glmer(model_a63a1_dyadic, data = subset(df_dyadic), nAGQ = 7, family = binomial(link = "logit"))
a63_c2 <- glmer(model_a63b_dyadic, data = subset(df_dyadic, copartisan_other == 1), nAGQ = 7, family = binomial(link = "logit"))
a63_c3 <- glmer(model_a63a1_dyadic, data = subset(df_dyadic, copartisan_subject == 1), nAGQ = 7, family = binomial(link = "logit"))
a63_c4 <- glmer(model_a63b_dyadic, data = subset(df_dyadic, nonpartisan_all == 1), nAGQ = 7, family = binomial(link = "logit"))

## Columns 1, 2, 4 
screenreg(list(a63_c1, a63_c2, a63_c4), 
       omit.coef = "ccode",
       stars = c(0.05), 
       custom.coef.names = c("Intercept", "Received Attacks", "Attacker Government t-1", "Delta Party Performance", "Party Performance",
                             "Political Knowledge", "Age", "Male", "Government t-1", "Received Attacks*Attacker Government t-1"),
       reorder.coef = c(2,3,10,4,5,6,7,8,9,1),
       custom.gof.names = c(NA, NA, NA, "Observations", "Groups", "Var: Groups (Intercept)"),
       custom.header = list("All" = 1, "Copartisans, target" = 2, "Nonpartisans" = 3))

## Column 3
screenreg(list(a63_c3), 
          omit.coef = "ccode",
          stars = c(0.05), 
          custom.coef.names = c("Intercept", "Party’s Attacks", "Attacker Government t-1", "Delta Party Performance", "Party Performance",
                                "Political Knowledge", "Age", "Male", "Government t-1", "Party’s Attacks*Attacker Government t-1"),
          reorder.coef = c(2,3,10,4,5,6,7,8,9,1),
          custom.gof.names = c(NA, NA, NA, "Observations", "Groups", "Var: Groups (Intercept)"),
          custom.header = list("Copartisans, attacker" = 1))

rm(model_a63a_monadic, model_a63b_monadic, model_a63c_dyadic)
rm(a63_c1, a63_c2, a63_c3, a63_c4)

################################### 
## Table A7-1: Replication of Table 3 With Leader and Party Valence
## Model specification
model_a71a_monadic <- as.formula(votedummy ~ share_nov_leader_all + share_nov_party_all + votet1_pollt1_diff + poll_campaign_start + pid_dummy_no + last_govt + polkno + age + gender + as.factor(ccode) + (1 | party_election))
model_a71b_monadic <- as.formula(votedummy ~ share_nov_leader_all + share_nov_party_all + votet1_pollt1_diff + poll_campaign_start + last_govt + polkno + age + gender + as.factor(ccode) + (1 | party_election))
model_a71c_dyadic  <- as.formula(votedummy_other ~ share_nov_leader_others + share_nov_party_others + share_nov_leader_all + share_nov_party_all + votet1_pollt1_diff_other + other_poll_campaign_start + polkno + age + gender + other_last_govt + as.factor(ccode) + (1 | party_election))

## Model estimation
a71_c1 <- glmer(model_a71a_monadic, data = subset(df_monadic), nAGQ = 7, family = binomial(link = "logit"))
a71_c2 <- glmer(model_a71b_monadic, data = subset(df_monadic, pid_dummy_no == 1), nAGQ = 7, family = binomial(link = "logit"))
a71_c3 <- glmer(model_a71c_dyadic,  data = subset(df_dyadic, copartisan_subject == 1), nAGQ = 7, family = binomial(link = "logit"))
a71_c4 <- glmer(model_a71b_monadic, data = subset(df_monadic, nonpartisan == 1), nAGQ = 7, family = binomial(link = "logit"))

## Results
## Columns 1, 2, 4 
screenreg(list(a71_c1, a71_c2, a71_c4), 
       omit.coef = "ccode",
       stars = c(0.05),
       custom.coef.names = c("Intercept", "Received Attacks (Leader)", "Received Attacks (Party)", "Delta Party Performance", "Party Performance",
                            "Copartisan", "Government t-1", "Political Knowledge", "Age", "Male"),
       reorder.coef = c(2, 3,4,5,6,7,8,9,10,1),
       custom.gof.names = c(NA, NA, NA, "Observations", "Groups", "Var: Groups (Intercept)"),
       custom.header = list("All" = 1, "Copartisans, target" = 2, "Nonpartisans" = 3))

## Column 3
screenreg(list(a71_c3), 
          omit.coef = "ccode",
          stars = c(0.05),
          custom.coef.names = c("Intercept", "Other's Attacks (Leader)", "Other's Attacks (Party)",
                                "Party's Attacks (Leader)", "Party's Attacks (Party)",
                                "Delta Party Performance", "Party Performance", 
                                "Political Knowledge", "Age", "Male", "Government t-1"),
          reorder.coef = c(4,5,2,3,6,7,11,8,9,10,1),
          custom.gof.names = c(NA, NA, NA, "Observations", "Groups", "Var: Groups (Intercept)"),
          custom.header = list("Copartisans, attacker" = 1))

rm(model_a71a_monadic, model_a71b_monadic, model_a71c_dyadic)
rm(a71_c1, a71_c2, a71_c3, a71_c4)

################################### 
## Table A8-1: Replication of Table 3 With Performance and Traits Valence
## Model specification
model_a81a_monadic <- as.formula(votedummy ~ share_nov_performance + share_nov_traits + votet1_pollt1_diff + poll_campaign_start + pid_dummy_no + last_govt + polkno + age + gender + as.factor(ccode) + (1 | party_election))
model_a81b_monadic <- as.formula(votedummy ~ share_nov_performance + share_nov_traits + votet1_pollt1_diff + poll_campaign_start + last_govt + polkno + age + gender + as.factor(ccode) + (1 | party_election))
model_a81c_dyadic  <- as.formula(votedummy_other ~ share_nov_traits_others + share_nov_performance_others + share_nov_performance + share_nov_traits + votet1_pollt1_diff_other + other_poll_campaign_start + polkno + age + gender + other_last_govt + as.factor(ccode) + (1 | party_election))

## Model estimation
a81_c1 <- glmer(model_a81a_monadic, data = subset(df_monadic), nAGQ = 7, family = binomial(link = "logit"))
a81_c2 <- glmer(model_a81b_monadic, data = subset(df_monadic, pid_dummy_no == 1), nAGQ = 7, family = binomial(link = "logit"))
a81_c3 <- glmer(model_a81c_dyadic,  data = subset(df_dyadic, copartisan_subject == 1), nAGQ = 7, family = binomial(link = "logit"))
a81_c4 <- glmer(model_a81b_monadic, data = subset(df_monadic, nonpartisan == 1), nAGQ = 7, family = binomial(link = "logit"))

## Results
## Columns 1, 2, 4 
screenreg(list(a81_c1, a81_c2, a81_c4), 
       omit.coef = "ccode",
       stars = c(0.05),
       custom.coef.names = c("Intercept", "Received Attacks (Performance)", "Received Attacks (Traits)", "Delta Party Performance", "Party Performance",
                             "Copartisan", "Government t-1", "Political Knowledge", "Age", "Male"),
       reorder.coef = c(2, 3,4,5,6,7,8,9,10,1),
       custom.gof.names = c(NA, NA, NA, "Observations", "Groups", "Var: Groups (Intercept)"),
       custom.header = list("All" = 1, "Copartisans, target" = 2, "Nonpartisans" = 3))

## Column 3
screenreg(list(a81_c3), 
          omit.coef = "ccode",
          stars = c(0.05),
          custom.coef.names = c("Intercept", "Others' Attacks (Traits)","Others' Attacks (Performance)",
                                "Party's Attacks (Performance)", "Party's Attacks (Traits)",
                                "Delta Party Performance", "Party Performance","Political Knowledge", "Age", "Male", "Government t-1"),
         reorder.coef = c(4,5,2,3,6,7,11,8,9,10,1),
          custom.gof.names = c(NA, NA, NA, "Observations", "Groups", "Var: Groups (Intercept)"),
          custom.header = list("Copartisans, attacker" = 1))


rm(model_a81a_monadic, model_a81b_monadic, model_a81c_dyadic)
rm(a81_c1, a81_c2, a81_c3, a81_c4)

# fin